<%@ page language="java" import="weaver.conn.RecordSet" pageEncoding="UTF-8" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.conn.RecordSetDataSource" %>
<%@ page import="weaver.general.TimeUtil" %>
<%@ page import="java.util.*" %>
<%@ page import="weaver.formmode.setup.ModeRightInfo" %>
<%@ page import="weaver.common.DateUtil" %>
<%--资产--%>
<%
    User user = HrmUserVarify.getUser(request, response);
    int userid = user.getUID();
    int formmodeid= 136;
    RecordSetDataSource dataSource = new RecordSetDataSource( "xfcs" );
    out.print( ">>>>>>>>开始执行>>>>>>"+"<br/>");
    try {
        dataSource.executeSql("select AssctID,AssctSumGuid,RegistInGuid,RegistOutGuid,ShiYongRenDisplayName,ReceiveDeptName,Price,Status from Misc_Assct_Detail where AssctSumGuid in (\n" +
                "select AssctSumGuid from Misc_Assct_Sum where CategoryGuid in (\n" +
                "select t2.CategoryGuid from Misc_Assct_Category t1 join  Misc_Assct_Category t2 \n" +
                "on t1.CategoryGuid=t2.ParentCategoryGuid where t1.ParentCategoryGuid='06034b69-d744-4219-ab66-4c5daff1b97e' ))");
        int count = 0;
        int counts = dataSource.getCounts();
        out.print( ">>>>>>>>查询记录总数为："+counts+"<br/>");
        while (dataSource.next()){
            Map dataMap = new HashMap(  );
            String AssctID = Util.null2String(dataSource.getString("AssctID"));
            String RegistInGuid = Util.null2String(dataSource.getString("RegistInGuid"));
            dataMap.put("zcbh",AssctID);//资产编号
            dataMap.put("zcbhguid",Util.null2String(dataSource.getString("AssctSumGuid")));//资产编号guidid
            Map<String, String> returnMap = getAssctSumName(RegistInGuid);
            dataMap.put("zclb",returnMap.get("AssctSumName"));//资产类别
            dataMap.put("zcmc",returnMap.get("zcmc"));//资产名称
            dataMap.put("dj",Util.null2String(dataSource.getString("Price")));//状态
            dataMap.put("zt",Util.null2String(dataSource.getString("Status")));//型号
            dataMap.put("syr",Util.null2String(dataSource.getString("ShiYongRenDisplayName")));//使用人
            dataMap.put("sybm",Util.null2String(dataSource.getString("ReceiveDeptName")));//使用部门
            dataMap.put("rkguid",RegistInGuid);//使用部门
            dataMap.put("ckguid",Util.null2String(dataSource.getString("RegistOutGuid")));//使用部门
            dataMap.put("tbrq", DateUtil.getCurrentDate() );//使用部门
            int dataId = insertModelData(formmodeid,userid,dataMap);
            out.print( ">>>>>>>>dataId："+dataId+"<br/>");
        }
        out.print( "执行成功主表数据记录数：>>>"+count+",主表数据总记录数：>>>"+counts);
    }catch (Exception e){
        out.print("执行异常，请查看日志！！！");
    }
%>
<%!
    //将数据插入表单建模
    public static int insertModelData(int modeid, int userid, Map map){
        int dataid=0;
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        //查询该模块表名
        String sql = "select b.tablename,a.formid from modeinfo a left join workflow_bill b on a.formid=b.id where a.id=?";
        rs.executeQuery(sql,modeid);
        rs.next();
        String tablename= Util.null2String(rs.getString("tablename"));
        String uuid = UUID.randomUUID().toString();
        String insSql = "insert into "+tablename
                +"(uuid,modedatacreater,modedatacreatedate,modedatacreatetime,formmodeid)values(?,?,?,?,?)";
        Object[] params = {uuid, userid, TimeUtil.getCurrentDateString(), TimeUtil.getOnlyCurrentTimeString(), modeid};
        if(rs.executeUpdate(insSql,params)){
            rs1.executeQuery("select id from "+tablename+" where uuid=?",uuid);
            rs1.next();
            dataid= Util.getIntValue(rs1.getString("id"));
            if(dataid>0){
                //遍历数据 进行update
                String updatesql="update "+tablename+" set ";
                Set<String> keySet = map.keySet();
                for(String key : keySet){
                    updatesql+=key+"='"+map.get(key).toString()+"',";
                }
                if(updatesql.endsWith(",")){
                    updatesql=updatesql.substring(0, updatesql.length()-1);
                    updatesql+=" where id="+dataid;
                    rs.executeUpdate(updatesql);
                }
                //进行权限重构
                getNewModeRight(userid, modeid, dataid);
            }
        }
        return dataid;
    }
    //建模权限重构
    public static void getNewModeRight(int userid, int formmodeid, int id) {
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.setNewRight( true );
        modeRightInfo.editModeDataShare( userid, formmodeid, id  );
    }

    public static Map<String, String> getAssctSumName(String RowGuid){
        Map<String, String> map = new HashMap<>();
        RecordSetDataSource dataSource = new RecordSetDataSource( "xfcs" );
        String sql = "select AssctSumName,PinPai+'-'+xinghao+'-'+GuiGe as zcmc" +
                " from Misc_Assct_RegisteIn where RowGuid='"+RowGuid+"'";
        dataSource.executeSql( sql );
        dataSource.next();
        String AssctSumName = Util.null2String(dataSource.getString("AssctSumName"));
        String zcmc = Util.null2String(dataSource.getString("zcmc"));
        map.put("AssctSumName",AssctSumName);
        map.put("zcmc",zcmc);
        return map;
    }
%>